<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Admin\Controller;

use Kcdns\Admin\Controller\CommonController;
use Think\Exception;
use Think\Model;

class JdController extends CommonController
{
	const LEVEL_VIP = [
		0 => [
			'level' => 0,
			'name' => '青铜',
			'money' => 3000,
			'days' => 15,
			'desc' => '入职15天为实习期 之后一个月内回款达到基础回款3000即可晋升为黑铁'
		],
		1 => [
			'level' => 1,
			'name' => '黑铁',
			'money' => 6000,
			'days' => 30,
			'desc' => '入职30天为实习期 之后一个月内回款达到基础回款4000即可晋升为黑铁'
		],
		2 => [
			'level' => 2,
			'name' => '白银',
			'money' => 9000,
			'days' => 45,
			'desc' => '入职45天为实习期 之后一个月内回款达到基础回款9000即可晋升为黑铁'
		],
		3 => [
			'level' => 3,
			'name' => '黄金',
			'money' => 12000,
			'days' => 60,
			'desc' => '入职60天为实习期 之后一个月内回款达到基础回款12000即可晋升为黑铁'
		],
		4 => [
			'level' => 4,
			'name' => '白金',
			'money' => 15000,
			'days' => 75,
			'desc' => '入职75天为实习期 之后一个月内回款达到基础回款15000即可晋升为黑铁'
		],
		5 => [
			'level' => 5,
			'name' => '钻石',
			'money' => 18000,
			'days' => 90,
			'desc' => '入职90天为实习期 之后一个月内回款达到基础回款18000即可晋升为黑铁'
		],
	];

	//0未发布1报名中2进行中3已删除4已终止5活动结束）
	const ACTIVITY_STATUS = [
		'0' => '未发布',
		'1' => '报名中',
		'2' => '进行中',
		'3' => '已删除',
		'4' => '已终止',
		'5' => '活动结束',
	];

    //结算
    public function indexNew()
    {

		$user = session('user_auth');
		$key = 'indexNew__' . $user['uid'];
		$cache = S($key);
		if ($cache) {
			$data =  json_decode($cache, true);
			$this->ajaxReturn([
				'code' => 1,
				'data' => $data,
				'msg' => '操作成功',
			]);
		} else {


			$m_where = isManager($user['group_id'], '', $user['uid']);
			$data['level_list'] = self::LEVEL_VIP;
			$member = M('member')->where(['uid' => $user['uid']])->find();
			$data['user_level'] = $member['level'];
			if ($member['level'] == 5) {
				$data['mubaio_level_text'] = "";
			} else {
				$cha_money = self::LEVEL_VIP[$member['level']]['money'] - $member['yugu_money'];
				$data['mubaio_level_text'] = "目标白银还差{$cha_money}完成升级";
			}


			$total = $this->getalldata();

			$data['yugu_data'] = [
				'today_yugu_money' => $total['today_yugu'],
				'yes_yugu_money' => $total['yestoday_yugu'],
				'benyue_yugu_money' => $total['thismon_yugu'],
				'shangyue_yugu_money' => $total['lastmon_yugu'],
			];
			//出轨未处理
			$where = ['state' => 2];
			if (isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
			$today_not_deal_num = M('zs_goods')->where($where)->count();
			//今日到期未处理
			$where = ['endTime' => date('Y.m.d')];
			if (isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
			$today_expire_num = M('zs_goods')->where($where)->count();
			//排期中产品数量
			$where = ['state' => 1];
			if (isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
			$paiqi_num = M('activity_goods')->where($where)->count();
			//合作中店铺数量
			$where = ['state' => 2];
			if (isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
			$hezuo_seller_num = M('activity_goods')->where($where)->count();
			$data['my_data'] = [
				'today_not_deal_num' => $today_not_deal_num ? $today_not_deal_num : 0,
				'today_expire_num' => $today_expire_num ? $today_expire_num : 0,
				'paiqi_num' => $paiqi_num ? $paiqi_num : 0,
				'hezuo_seller_num' => $hezuo_seller_num ? $hezuo_seller_num : 0,
			];


			//寻找招商组
			/*$group = M('auth_group')->field('*')->where(['is_zs' => 1])->select();
            $gid_arr = array_column($group, 'id');
            $group_access = M('auth_group_access')->where(['group_id' => ['in', $gid_arr]])->select();
            $uid_arr = array_column($group_access, 'uid');
            $where = ['uid' => ['in', $uid_arr]];
            $member_list =  M('member')->field('uid, nickname as name')->where($where)->select();
            $today = date("Y-m-d");
            foreach($member_list as &$item){
                $where = ['order_time'=>['egt',$today],'validCode'=>['in','15,16,17,18'], 'mid' => $item['uid']];
                $flag =  M('zs_order_pay')->where($where)->count();
                $item['order_num'] = $flag?$flag:0;
                $where = ['order_time'=>['egt',$today],'validCode'=>['in','16,17,18'], 'mid' => $item['uid']];
                $flag =  M('zs_order_pay')->where($where)->sum('estimateFee');
                $item['order_yugu_money'] = $flag?$flag:0.00;
                $where = ['order_time'=>['egt',$today],'validCode'=>['in','15,16,17,18'], 'mid' => $item['uid']];
                $flag =  M('zs_order_pay')->where($where)->sum('estimateCosPrice');
                $item['order_money'] = $flag?$flag:0.00;
            }
            // 取得列的列表
            foreach ($member_list as $key => $row) {
                $distance[$key] = $row['order_yugu_money'];
            }
            array_multisort($distance, SORT_DESC, $member_list);
            $i = 1;
            foreach($member_list as &$item){
                $item['ranking'] = $i;
                $i++;
            }
            $data['ranking'] = $member_list;*/
			$echart = self::echartData();
			$data['time'] = $echart['time'];
			$data['ordernum'] = $echart['ordernum'];
			$data['auctionNum'] = $echart['auctionNum'];
			$data['estimateServiceFee'] = $echart['estimateServiceFee'];
			$data['estimateCosPrice'] = $echart['estimateCosPrice'];
			$echart_right = self::echartRight();
			$data['echart_right'] = $echart_right;
			S($key, json_encode($data), ['expire' => 600]);
			$this->ajaxReturn([
				'code' => 1,
				'data' => $data,
				'msg' => '操作成功',
			]);
		}
    }

	public function echartData(){
		
		$date_ymd = date('Y-m-d');
		$data['time'] = ["{$date_ymd} 00:00:00", "{$date_ymd} 01:00:00", "{$date_ymd} 02:00:00", "{$date_ymd} 03:00:00", "{$date_ymd} 04:00:00", "{$date_ymd} 05:00:00", "{$date_ymd} 06:00:00", "{$date_ymd} 07:00:00", "{$date_ymd} 08:00:00", "{$date_ymd} 09:00:00", "{$date_ymd} 10:00:00", "{$date_ymd} 11:00:00", "{$date_ymd} 12:00:00", "{$date_ymd} 13:00:00", "{$date_ymd} 14:00:00", "{$date_ymd} 15:00:00", "{$date_ymd} 16:00:00", "{$date_ymd} 17:00:00", "{$date_ymd} 18:00:00", "{$date_ymd} 19:00:00", "{$date_ymd} 20:00:00", "{$date_ymd} 21:00:00", "{$date_ymd} 22:00:00", "{$date_ymd} 23:00:00"];
		$data['ordernum'] = [];
		$data['auctionNum'] = [];
		$data['estimateServiceFee'] = [];
		$data['estimateCosPrice'] = [];

		$tmp = '';
		$nowtime = date('Y-m-d H:i:s');
		foreach($data['time'] as $k => $v){
			$start = date('Y-m-d H:i:s', strtotime($v)-3600);
			$where = ['order_time' => ['between', [$start, $v]], 'is_pay' => 1];
			$where['validCode'] = ['in', '16,17,18'];
			//订单数量
			$order_num = M('zs_order_pay')->where($where)->count();
			$data['ordernum'][] = $order_num?$order_num:0;
			//商品数
			$goods_num = M('zs_order_pay')->where($where)->group('skuId')->count();
			$data['auctionNum'][] = $goods_num?$goods_num:0;
			//付款金额
			$estimateCosPrice = M('zs_order_pay')->where($where)->sum('estimateCosPrice');
			$data['estimateServiceFee'][] = $estimateCosPrice?$estimateCosPrice:0.00;
			//付款服务费
			$estimateFee = M('zs_order_pay')->where($where)->sum('estimateFee');
			$data['estimateCosPrice'][] = $estimateFee?$estimateFee:0.00;
		}
		return $data;
	}

	public function echartRight(){
		//今日
		$where = ['order_time' => ['between', [date('Y-m-d 00:00:00'), date('Y-m-d H:i:s')]], 'is_pay' => 1];
		$where['validCode'] = ['in', '16,17,18'];
		//订单数量
		$order_num = M('zs_order_pay')->where($where)->count();
		$today['ordernum'] = $order_num?$order_num:0;
		//商品数
		$goods_num = M('zs_order_pay')->where($where)->group('skuId')->count();
		$today['auctionNum'] = $goods_num?$goods_num:0;
		//付款金额
		$estimateCosPrice = M('zs_order_pay')->where($where)->sum('estimateCosPrice');
		$today['estimateServiceFee'] = $estimateCosPrice?$estimateCosPrice:0.00;
		//付款服务费
		$estimateFee = M('zs_order_pay')->where($where)->sum('estimateFee');
		$today['estimateCosPrice'] = $estimateFee?$estimateFee:0.00;
		//昨日
		$where = [
			'order_time' => ['between', [date('Y-m-d 00:00:00', strtotime('-1days')), date('Y-m-d 23:59:59', strtotime('-1days'))]],
			'validCode' => ['in', '16,17,18'],
			'is_pay' => 1,
		];
		$order_num = M('zs_order_pay')->where($where)->count();
		$yestoday['ordernum'] = $order_num?$order_num:0;
		//付款服务费
		$estimateFee = M('zs_order_pay')->where($where)->sum('estimateFee');
		$yestoday['estimateCosPrice'] = $estimateFee?$estimateFee:0.00;
		return ['echart_today' => $today, 'echart_yestoday' => $yestoday];
	}

	public function indexRanking(){

		$page_num = I('post.page_num') ?: 1;
		$page_size = I('post.page_size') ?: PAGE_SIZE;
		$gid = I('post.gid');
		$type = I('post.type'); //[1今天|2昨天|3本月|4上月]
		$today = date("Y-m-d");
		$yesterday = date("Y-m-d", strtotime(date("Y-m-d"))-86400);
		$thismonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
		$thismonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
		$lastmonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
		$lastmonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
		$where =  ['validCode'=>['in','15,16,17,18']];
		if($type == 1){
			$where['order_time'] = ['egt',$today];
		}elseif($type ==2){
			$where['order_time'] = ['between',[$yesterday,$today]];
		}elseif($type ==3){
			$where['order_time'] = ['between',[$thismonth_start,$thismonth_end]];
		}elseif($type ==4){
			$where['order_time'] = ['between',[$lastmonth_start,$lastmonth_end]];
		}
		if($gid){
			$group = M('auth_group_access')->where(['group_id' => $gid])->select();
			$uid_arr = array_column($group, 'uid');
			$where['mid'] = ['in', $uid_arr];
		}

		$member_list =  M('member')->field('uid, nickname as name')->where($where)->select();
		$today = date("Y-m-d");
		$total_money = 0;
		foreach($member_list as &$item){
			$where['mid'] = $item['uid'];
			$flag =  M('zs_order_pay')->where($where)->count();
			$item['order_num'] = $flag?$flag:0;
			$flag =  M('zs_order_pay')->where($where)->sum('estimateFee');
			$item['order_yugu_money'] = $flag?$flag:0.00;
			$flag =  M('zs_order_pay')->where($where)->sum('estimateCosPrice');
			$item['order_money'] = $flag?$flag:0.00;
			$total_money += $item['order_yugu_money'];
		}



		// 取得列的列表
		foreach ($member_list as $key => $row) {
			$distance[$key] = $row['order_yugu_money'];
		}


		array_multisort($distance, SORT_DESC, $member_list);

		$i = 1;
		foreach($member_list as &$item){
			$item['ranking'] = $i;
			$i++;
		}
		$data['ranking'] = $member_list;
		$data['total_money'] = $total_money;
		$this->ajaxReturn([
			'code' => 1,
			'data' => $data,
			'msg' => '操作成功',
		]);
	}

	public function getGidList(){
		$group = M('auth_group')->field('id, title')->where(['is_zs' => 1])->order('id desc')->select();
		$this->ajaxReturn([
			'code' => 1,
			'data' => $group,
			'msg' => '操作成功',
		]);
	}

	public function getalldata(){
		$user = session('user_auth');
		$mid_where = isManager($user['group_id'], '', $user['uid']);
		$key = 'getalldata_cache122321_'.$user['uid'];
		$cache = S($key);
		if($cache){
			return json_decode($cache ,true);
		}
		$today = date("Y-m-d");
		$yesterday = date("Y-m-d", strtotime(date("Y-m-d"))-86400);
		$thismonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
		$thismonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
		$lastmonth_start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
		$lastmonth_end = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
		$where = array_merge($mid_where, ['order_time'=>['egt',$today],'validCode'=>['in','15,16,17,18']]);
		$today_input_count =  M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where, ['order_time'=>['egt',$today],'validCode'=>['in','16,17,18']]);
		$today_order_count = M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where, ['order_time'=>['egt',$today],'validCode'=>['in','16,17,18']]);
		$today_money = M('zs_order_pay')->where($where)->sum('estimateCosPrice');
		$where = array_merge($mid_where, ['order_time'=>['egt',$today],'validCode'=>['in','16,17,18']]);
		$today_yugu = M('zs_order_pay')->where($where)->sum('estimateFee');
		$where = array_merge($mid_where, ['order_time'=>['between',[$yesterday,$today]],'validCode'=>['in','15,16,17,18']]);
		$yestoday_order_count = M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where,['order_time'=>['between',[$yesterday,$today]],'validCode'=>['in','16,17,18']]);
		$yestoday_yugu = M('zs_order_pay')->where($where)->sum('estimateFee');
		$where = array_merge($mid_where, ['order_time'=>['between',[$thismonth_start,$thismonth_end]],'validCode'=>['in','15,16,17,18']]);
		$thismon_input = M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where, ['order_time'=>['between',[$thismonth_start,$thismonth_end]],'validCode'=>['in','16,17,18']]);
		$thismon_order = M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where, ['order_time'=>['between',[$thismonth_start,$thismonth_end]],'validCode'=>['in','16,17,18']]);
		$thismon_yugu = M('zs_order_pay')->where($where)->sum('estimateFee');
		$where = array_merge($mid_where, ['order_time'=>['between',[$lastmonth_start,$lastmonth_end]],'validCode'=>['in','15,16,17,18']]);
		$lastmon_input = M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where, ['order_time'=>['between',[$lastmonth_start,$lastmonth_end]],'validCode'=>['in','16,17,18']]);
		$lastmon_order = M('zs_order_pay')->where($where)->count();
		$where = array_merge($mid_where, ['order_time'=>['between',[$lastmonth_start,$lastmonth_end]],'validCode'=>['in','16,17,18']]);
		$lastmon_yugu = M('zs_order_pay')->where($where)->sum('estimateFee');


		$data = [
			'today_input_count'=>$today_input_count?$today_input_count:0,
			'today_order_count'=>$today_order_count?$today_order_count:0,
			'today_money'=>$today_money?$today_money:0.00,
			'today_yugu'=>$today_yugu?$today_yugu:0.00,
			'yestoday_order_count'=>$yestoday_order_count?$yestoday_order_count:0,
			'yestoday_yugu'=>$yestoday_yugu?$yestoday_yugu:0.00,
			'thismon_input'=>$thismon_input?$thismon_input:0,
			'thismon_order'=>$thismon_order?$thismon_order:0,
			'thismon_yugu'=>$thismon_yugu?$thismon_yugu:0.00,
			'lastmon_input'=>$lastmon_input?$lastmon_input:0,
			'lastmon_order'=>$lastmon_order?$lastmon_order:0,
			'lastmon_yugu'=>$lastmon_yugu?$lastmon_yugu:0.00,
		];
		S($key, json_encode($data), ['expire' => 300]);
		return $data;
	}

	//店铺列表
	public function sellerList(){
		$page_num = I('post.page_num') ?: 1;
		$page_size = I('post.page_size') ?: PAGE_SIZE;
		$shopId = I('post.shopId');
		$seller_nickname = I('post.seller_nickname');
		$seller_title = I('post.seller_title');
		$qq = I('post.qq');
		$weixin_name = I('post.weixin_name');
		$mobile = I('post.mobile');
		$remark = I('post.remark');
		$mid = I('post.mid');


		$where = [];
		if($shopId)$where['shopId'] = $shopId;
		if($seller_nickname)$where['seller_nickname'] = $seller_nickname;
		if($seller_title)$where['seller_title'] = $seller_title;
		if($qq)$where['qq'] = $qq;
		if($weixin_name)$where['weixin_name'] = $weixin_name;
		if($mobile)$where['mobile'] = $mobile;
		if($remark)$where['remark'] = $remark;
		if($mid)$where['mid'] = $mid;


		$user = session('user_auth');
		$m_where = isManager($user['group_id'], '', $user['uid']);
		if(isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
		$seller_list = (array)M('zs_seller')
			->field('id, shopId, seller_nickname,seller_title,url,is_hezuo,qq,weixin_name,mobile,create_time, mid, allot')
			->where($where)->order('id asc')->page($page_num, $page_size)->select();
		$count = M('zs_seller')->where($where)->count();
		foreach($seller_list as &$item){
			$member = M('member')->where(['uid' => $item['mid']])->find();
			$item['m_nickname'] = $member['nickname']?$member['nickname']:'';
			$item['url'] = $item['url']?$item['url']:"https://mall.jd.com/index-{$item['shopId']}.html";
		}
		$this->ajaxReturn([
			'code' => 1,
			'data' => $seller_list,
			'msg' => '操作成功',
			'page_num' => $page_num,
			'page_size' => $page_size,
			'page_count' => $count,
			'page_total' => ceil($count / $page_size),
		]);
	}

	//分配
	public function bindSeller(){
		$id = I('post.id');
		$mid = I('post.mid');
		if(!$id || !$mid)
		$model = new Model();
		try{
			M()->startTrans();
			$member = M('member')->where(['uid' => $mid])->find();
			if(!isset($member['uid']))  throw new \Exception('分配人员不存在');
			$activity = M('zs_activity')->where(['id' => $id])->find();
			if($activity['allot'] == 1) throw new \Exception('已分配');
			$flag = M('zs_activity')->where(['id' => $id])->save(['mid' => $mid, 'allot' => 1]);
			if($flag ===false)  throw new \Exception('商品分配失败[-1]');
			$flag = M('zs_goods')->where(['activityId' => $activity['taobao_act_id'], 'allot' => 2])->save(['mid' => $mid, 'allot' => 1]);
			if($flag ===false)  throw new \Exception('商品分配失败[-2]');
			/*$flag = M('zs_seller')->where(['taobao_act_id' => $activity['taobao_act_id'], 'allot' => 2])->save(['mid' => $mid, 'allot' => 1]);
			if($flag ===false)  throw new \Exception('商家分配失败[-3]');*/
			$flag = M('zs_order_pay')->where(['taobao_act_id' => $activity['taobao_act_id'], 'allot' => 2])->save(['mid' => $mid, 'allot' => 1]);
			if($flag ===false)  throw new \Exception('订单分配失败[-1]');
			M()->commit();
			//记录日志
			$log = [
				'title' => '分配操作',
				'content' => "分配活动{$activity['activity_title']}({$activity['taobao_act_id']})给{$member['nickname']}({$member['uid']})",
			];
			addLog($log);
			$this->ajaxReturn([
				'code' => 1,
				'msg' => '操作成功',
			]);
		} catch (\Exception $e) {
			M()->rollback();
			$msg = $e->getMessage();
			$this->ajaxReturn([
				'code' => -1,
				'msg' => $msg,
			]);
		}

	}

	//绑定人列表
	public function binderList(){

		$user = session('user_auth');
		$group = M('auth_group')->where(['id' => $user['group_id']])->find();
		/*if($group['is_zs'] == 0) {
			$this->ajaxReturn([
				'code' => -1,
				'msg' => '您不是招商组管理员',
			]);
		}*/


		$list = (array)M('auth_group_access')->where(['is_zs' => 1])->select();
		$uid_arr = array_column($list, 'uid');

		$member_list = M('member')->field('uid,nickname,mobile')->where(['uid' => ['in', $uid_arr]])->select();
		$this->ajaxReturn([
			'code' => 1,
			'data' => $member_list,
			'msg' => '操作成功',
		]);

	}

	//预估效果
	public function yuguxiaoguo(){

		$id = I('post.id');
		if(!$id) $this->ajaxReturn([
			'code' => -1,
			'msg' => '商家id不能为空',
		]);
		$seller = M('zs_seller')->where(['id' => $id])->find();
		if(!isset($seller['id'])) if(!$id) $this->ajaxReturn([
			'code' => -1,
			'msg' => '商家不存在',
		]);

		$today = date("Y-m-d");
		$yesterday = date("Y-m-d", strtotime(date("Y-m-d"))-86400);
		//今日
		$where = ['shopId' => $seller['shopId']];
		$flag = M('zs_order_pay')->where($where)->count();
		$data['total_order_num'] =$flag?$flag:0;
		$flag = M('zs_order_pay')->where(['shopId' => $seller['shopId']])->sum('estimateFee');
		$data['total_yugu_money'] =$flag?$flag:0;
		$where = [
			'shopId' => $seller['shopId'],
			'order_time'=>['egt',$today],
			'validCode'=>['in','16,17,18'],
		];
		$flag = M('zs_order_pay')->where($where)->sum('estimateFee');
		$data['today_yugu_money'] = $flag?$flag:0.00;
		$flag = M('zs_order_pay')->where($where)->count();
		$data['today_order_num'] = $flag?$flag:0;
		//昨日
		$where = [
			'shopId' => $seller['shopId'],
			'order_time'=>['between',[$yesterday,$today]],
			'validCode'=>['in','16,17,18'],
		];
		$flag = M('zs_order_pay')->where($where)->sum('estimateFee');
		$data['yes_order_num'] = $flag?$flag:0.00;
		$flag = M('zs_order_pay')->where($where)->count();
		$data['yes_yugu_money'] = $flag?$flag:0;

		//近七日
		$qiday = date("Y-m-d", strtotime(date("Y-m-d"))-86400*7);
		$where = [
			'shopId' => $seller['shopId'],
			'order_time'=>['between',[$qiday,$today]],
			'validCode'=>['in','16,17,18'],
		];
		$flag = M('zs_order_pay')->where($where)->sum('estimateFee');
		$data['qi_order_num'] = $flag?$flag:0.00;
		$flag = M('zs_order_pay')->where($where)->count();
		$data['qi_yugu_money'] = $flag?$flag:0;

		$this->ajaxReturn([
			'code' => 1,
			'data' => $data,
			'msg' => '操作成功',
		]);
	}

	//活动列表
	public function activityList(){

		$page_num = I('post.page_num') ?: 1;
		$page_size = I('post.page_size') ?: PAGE_SIZE;
		$taobao_act_id = I('post.taobao_act_id');
		$activity_title = I('post.activity_title');
		$mid = I('post.mid');
		$activityStatus = I('post.activityStatus');
		$where = [];
		if($activity_title)$where['activity_title'] = ['like', "%{$activity_title}%"];
		if($taobao_act_id)$where['taobao_act_id'] = $taobao_act_id;
		$user = session('user_auth');
		$m_where = isManager($user['group_id'], '', $user['uid']);
		if(isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
		if($mid)$where['mid'] = $mid;
		$seller_list = (array)M('zs_activity')
			->field('id, activity_title,mid, start_time,end_time,remark,taobao_act_id,create_time,state, allot, activityStatus')
			->where($where)->order('start_time desc,end_time desc')->page($page_num, $page_size)->select();
		$count = M('zs_activity')->where($where)->count();
		foreach($seller_list as &$item){
			$member = M('member')->where(['uid' => $item['mid']])->find();
			$item['m_nickname'] = $member['nickname']?$member['nickname']:'';
			$item['activityStatus_label'] = self::ACTIVITY_STATUS[$item['activityStatus']];

		}
		$this->ajaxReturn([
			'code' => 1,
			'data' => $seller_list,
			'msg' => '操作成功',
			'page_num' => $page_num,
			'page_size' => $page_size,
			'page_count' => $count,
			'page_total' => ceil($count / $page_size),
		]);
	}


	//工资列表
	public function salaryList(){
		$page_num = I('post.page_num') ?: 1;
		$page_size = I('post.page_size') ?: PAGE_SIZE;
		$mid = I('post.mid');
		$report_data = I('post.report_data');
		$where = [];
		if($mid)$where['mid'] = $mid;
		if($report_data)$where['report_data'] = $report_data;
		$user = session('user_auth');
		$m_where = isManager($user['group_id'], '', $user['uid']);
		if(isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
		$seller_list = (array)M('zs_salary')
			->where($where)->order('id desc')->page($page_num, $page_size)->select();
		$count = M('zs_salary')->where($where)->count();
		foreach($seller_list as &$item){
			$member = M('member')->where(['uid' => $item['mid']])->find();
			$item['m_nickname'] = $member['nickname']?$member['nickname']:'';
		}
		$this->ajaxReturn([
			'code' => 1,
			'data' => $seller_list,
			'msg' => '操作成功',
			'page_num' => $page_num,
			'page_size' => $page_size,
			'page_count' => $count,
			'page_total' => ceil($count / $page_size),
		]);
	}

	public function editSeller(){
		$id = I('post.id');
		$seller_nickname = I('post.seller_nickname');
		$seller_title = I('post.seller_title');
		$url = I('post.url');
		$qq = I('post.qq');
		$weixin_name = I('post.weixin_name');
		$is_hezuo = I('post.is_hezuo');
		$mobile = I('post.mobile');
		$remark = I('post.remark');
		$operation = I('post.operation');
		if(!$id) 	$this->ajaxReturn([
			'code' => -1,
			'msg' => 'id必须填写',
		]);
		$seller = M('zs_seller')->where(['id' => $id])->find();
		if(!isset($seller['id'])) 	$this->ajaxReturn([
			'code' => -1,
			'msg' => '商家不存在',
		]);
		if(!isset($seller_title)) 	$this->ajaxReturn([
			'code' => -1,
			'msg' => '店铺名称必须填写',
		]);
		if(!isset($url)) 	$this->ajaxReturn([
			'code' => -1,
			'msg' => '店铺地址必须填写',
		]);
		if(!isset($seller_nickname)) 	$this->ajaxReturn([
			'code' => -1,
			'msg' => '店主昵称必须填写',
		]);
		if(!isset($is_hezuo)) 	$this->ajaxReturn([
			'code' => -1,
			'msg' => '是否合作必须勾选',
		]);
		$save = [
			'seller_nickname' => $seller_nickname,
			'seller_title' => $seller_title,
			'url' => $url,
			'is_hezuo' => $is_hezuo,
		];
		if($qq) $save['qq'] = $qq;
		if($weixin_name) $save['weixin_name'] = $weixin_name;
		if($mobile) $save['mobile'] = $mobile;
		if($remark) $save['remark'] = $remark;
		if($operation) $save['operation'] = $operation;
		$flag = M('zs_seller')->where(['id' => $id])->save($save);
		if($flag == false){
			$this->ajaxReturn([
				'code' => 1,
				'msg' => '操作成功',
			]);
		}else{
			$this->ajaxReturn([
				'code' => -1,
				'msg' => '操作失败',
			]);
		}

	}

	public function sellerDetail(){

		$id = I('post.id') ?: 1;
		$seller = M('zs_seller')->where(['id' => $id])->find();
		if($seller){
			$this->ajaxReturn([
				'code' => 1,
				'msg' => '操作成功',
				'data' =>$seller
			]);
		}else{
			$this->ajaxReturn([
				'code' => -1,
				'msg' => '操作失败',
			]);
		}
	}

	public function goodsList(){

		$page_num = I('post.page_num') ?: 1;
		$page_size = I('post.page_size') ?: PAGE_SIZE;
		$shopId = I('post.shopId');
		$skuId = I('post.skuId');
		$title = I('post.title');
		$state = I('post.state');
		$activityId = I('post.activityId');
		$s_time = I('post.s_time');
		$e_time = I('post.e_time');
		$type = I('post.type');  //0全部 1待挖单处理 2昨日过期 3今日过期 4明日过期
		$where = [];
		if($shopId)$where['shopId'] = $shopId;
		if($activityId)$where['activityId'] = $activityId;
		if($skuId)$where['skuId'] = $skuId;
		if($title)$where['title'] = $title;
		if($s_time)$where['s_time'] = $s_time;
		if($e_time)$where['e_time'] = $e_time;

		if($type == 1){
			$where['state'] = 2;
		}elseif($type == 2){
			$where['endTime'] = date('Y.m.d', strtotime('-1days'));
		}elseif($type == 3){
			$where['endTime'] = date('Y.m.d');
		}elseif($type == 4){
			$where['endTime'] = date('Y.m.d', strtotime('+1days'));
		}else{

		}

		$user = session('user_auth');
		$m_where = isManager($user['group_id'], '', $user['uid']);
		if(isset($m_where['mid'])) $where['mid'] = $m_where['mid'];
		if($state)   $where['state'] = $state;
		if($shopId)   $where['shopId'] = $shopId;
		if($skuId)   $where['skuId'] = $skuId;

		$seller_list = (array)M('zs_goods')
			->field('id, skuName,shopName,shopId,commisionRatioWl,price,imgUrl,goods_url,mid, startTime,endTime,status,activityId,create_time,commissionRate,serviceRate, allot')
			->where($where)
			->order('id asc')
			->page($page_num, $page_size)
			->select();
		$count = M('zs_goods')->where($where)->count();
		foreach($seller_list as &$item){
			$member = M('member')->where(['uid' => $item['mid']])->find();
			$item['m_nickname'] = $member['nickname']?$member['nickname']:'';

		}

		//统计
		$total = self::totalGoods();
		$result = [
			'code' => 1,
			'all_num' => $total['all_num'],
			'dai_goods_num' => $total['dai_goods_num'],
			'yes_goods_num' => $total['yes_goods_num'],
			'today_goods_num' => $total['today_goods_num'],
			'tomo_goods_num' => $total['tomo_goods_num'],
			'data' => $seller_list,
			'msg' => '操作成功',
			'page_num' => $page_num,
			'page_size' => $page_size,
			'page_count' => $count,
			'page_total' => ceil($count / $page_size),
		];
		$result = array_merge($result, $total);
		$this->ajaxReturn($result);
	}

	const ORDER_STATE = [
		'1' => '未知',
		'2' => '无效/拆单',
		'3' => '无效/取消',
		'4' => '无效/京东帮帮主订单',
		'5' => '无效/账号异常',
		'6' => '无效/赠品类目不返佣',
		'7' => '无效/校园订单',
		'8' => '无效/企业订单',
		'9' => '无效/团购订单',
		'10' => '无效/开增值税专用发票订单',
		'11' => '无效/乡村推广员下单',
		'12' => '无效/自己推广自己下单',
		'13' => '无效/违规订单',
		'14' => '无效/来源与备案网址不符',
		'15' => '待付款',
		'16' => '已付款',
		'17' => '已完成',
		'18' => '已结算',
	];

	public function orderList(){

		$page_num = I('post.page_num') ?: 1;
		$page_size = I('post.page_size') ?: PAGE_SIZE;
		$taobao_act_id = I('post.taobao_act_id');
		$shopId = I('post.shopId');
		$skuId = I('post.skuId');
		$order_no = I('post.order_no');
		$skuShopName = I('post.skuShopName');
		$mid = I('post.mid');
		$activityId = I('post.activityId');
		$time1 = I('start_time');
		$time2 = I('end_time');
		$type = I('post.type');  //0全部 1待挖单处理 2昨日过期 3今日过期 4明日过期
		$where = [];
		if($mid)$where['mid'] = $mid;
		if($shopId)$where['shopId'] = $shopId;
		if($activityId)$where['activityId'] = $activityId;
		if($skuId)$where['skuId'] = $skuId;
		if($taobao_act_id)$where['taobao_act_id'] = $taobao_act_id;
		if($order_no)$where['order_no'] = $order_no;
		if($skuShopName)$where['skuShopName'] = $skuShopName;

		if($type == 1){
			$where['state'] = 2;
		}elseif($type == 2){
			$where['endTime'] = date('Y.m.d', strtotime('-1days'));
		}elseif($type == 3){
			$where['endTime'] = date('Y.m.d');
		}elseif($type == 4){
			$where['endTime'] = date('Y.m.d', strtotime('+1days'));
		}else{

		}
		$where['is_pay'] = 1;
		$where['validCode'] = ['in', '16,17,18'];

		$today = date("Y-m-d 00:00:00", strtotime('-7days'));
		if ($time1 && $time2) {
			$time1 = $time1 . ' 0:0:0';
			$time2 = $time2 . ' 23:59:59';
			$where['order_time'] = [['between', [$time1, $time2]]];
		} else {
			$where['order_time'] = [['egt', $today]];
		}

		$user = session('user_auth');
		$m_where = isManager($user['group_id'], '', $user['uid']);
		if(isset($m_where['mid'])) $where['mid'] = $m_where['mid'];

		$seller_list = (array)M('zs_order_pay')
			->field('*')
			->where($where)->order('order_time desc')->page($page_num, $page_size)->select();
		$count = M('zs_order_pay')->where($where)->count();
		foreach($seller_list as &$item){
			$member = M('member')->where(['uid' => $item['mid']])->find();
			$item['m_nickname'] = $member['nickname']?$member['nickname']:'';
			$item['order_status_label'] = !empty(self::ORDER_STATE[$item['validCode']])?self::ORDER_STATE[$item['validCode']]:'';
		}

		//订单数量
		$order_num = $count;
		//商品数
		$goods_num = M('zs_order_pay')->where($where)->group('skuId')->count();
		$goods_num = $goods_num?$goods_num:0;
		//付款金额
		$estimateCosPrice = M('zs_order_pay')->where($where)->sum('estimateCosPrice');
		$estimateCosPrice = $estimateCosPrice?$estimateCosPrice:0.00;
		//付款服务费
		$estimateFee = M('zs_order_pay')->where($where)->sum('estimateFee');
		$estimateFee = $estimateFee?$estimateFee:0.00;
		$result = [
			'code' => 1,
			'order_num' => $order_num,
			'goods_num' => $goods_num,
			'estimateCosPrice' => $estimateCosPrice,
			'estimateFee' => $estimateFee,
			'data' => $seller_list,
			'msg' => '操作成功',
			'page_num' => $page_num,
			'page_size' => $page_size,
			'page_count' => $count,
			'page_total' => ceil($count / $page_size),
		];
		$this->ajaxReturn($result);
	}

	public function totalGoods(){
		//全部
		$where = [];
		$user = session('user_auth');
		$key = 'totalGoods_'.$user['uid'];
		$cache = S($key);
		if($cache){
			return json_decode($cache, true);
		}else{
			//		var_dump($user);exit();
			$m_where = isManager($user['group_id'], '', $user['uid']);
			if(isset($m_where['mid'])) $where = array_merge($where, isManager($user['group_id'], '', $user['uid']));
			$flag = M('zs_goods')->where($where)->count();
			$result['all_num'] = $flag?$flag:0;
			//待处理的挖单数量
			$where = [
				'state' => 2
			];
			if(isset($m_where['mid'])) $where['mid'] =  $m_where['mid'];
			$flag = M('zs_goods')->where($where)->count();
			$result['dai_goods_num'] = $flag?$flag:0;
			//昨天过期
			$where = [
				'endTime' => date('Y.m.d', strtotime('-1days'))
			];
			if(isset($m_where['mid'])) $where['mid'] =  $m_where['mid'];
			$flag = M('zs_goods')->where($where)->count();
			$result['yes_goods_num'] = $flag?$flag:0;
			//今日过期
			$where = [
				'endTime' => date('Y.m.d')
			];
			if(isset($m_where['mid'])) $where['mid'] =  $m_where['mid'];
			$flag = M('zs_goods')->where($where)->count();
			$result['today_goods_num'] = $flag?$flag:0;
			//tomo_goods_num 明日过期
			$where = [
				'endTime' => date('Y.m.d', strtotime('+1days'))
			];
			if(isset($m_where['mid'])) $where['mid'] =  $m_where['mid'];
			$flag = M('zs_goods')->where($where)->count();
			$result['tomo_goods_num'] = $flag?$flag:0;
			S($key, json_encode($result), ['expire' => 3600]);
			return $result;
		}

	}


	public function tokenModify(){
		$data =  M('zs_jd_cookier')->where(['id' => 1])->find();
		if($data['state'] == 1)  $this->ajaxReturn([
			'code' => 0,
			'msg' => 'token未失效',
		]);
		$content = I('post.token');
		$flag =  M('zs_jd_cookier')->where(['id' => 1])->save([
			'cookier_value' => $content,
			'update_time' => date('Y-m-d H:i:s'),
			'state' => 1,
			'count' => 0
		]);
		if($flag === false)  $this->ajaxReturn([
			'code' => 1,
			'msg' => '操作失败',
		]);
		$this->ajaxReturn([
			'code' => 1,
			'msg' => '操作成功',
		]);
	}

	public function getToken(){

		$data = M('zs_jd_cookier')->where(['id' => 1])->find();
		$this->ajaxReturn([
			'code' => 1,
			'msg' => '操作成功',
			'data' => $data,
		]);
	}


	public function test(){
		$user = session('user_auth');var_dump($user);exit();
		var_dump($_SERVER['REDIRECT_URL']);exit();
	}

}